app.service("liveService", function($http, $q){
	var liveMenus = {};
	//生活菜单
	var menus = [];
	//生活区模块
	var liveModule = [];
	//生活区标题活动
	var liveAdvert = [];
	//生活每日尖货数据
	var liveDay = [];
	//生活餐具
	var liveDinner = [];
	//清洁专区数据
	var liveClear = [];
	//数码家电
	var liveTV = [];
	//健康成长
	var liveGrow = [];
	var def = $q.defer();
	$http.get('./data/liveMenu.json').success(function(data){
		//菜单分类数据
		menus.push(data.default_name);
		for(var i = 0 ,len = data.sub_cate_list.length;i < len;i++){
			menus.push(data.sub_cate_list[i].name);
		}
		liveMenus.menus = menus;
		
	}) ;
	$http.get('./data/live.json').success(function(data){
		//生活区模块
		for(var i = 0 ;i < 2;i++){
			for(var j = 0; j < 2;j++){
				liveModule.push(data.data.module_ads.multi_block[i].data[0].child[j].pic);
			}
		}
		//活动的标题分类
		liveAdvert.push(data.data.module_ads.multi_block[2].data[0].child[0].pic);	
		liveAdvert.push(data.data.module_ads.multi_block[4].data[0].child[0].pic);
		liveAdvert.push(data.data.module_ads.multi_block[7].data[0].child[0].pic);
		liveAdvert.push(data.data.module_ads.multi_block[10].data[0].child[0].pic);
		liveAdvert.push(data.data.module_ads.multi_block[13].data[0].child[0].pic);
		liveAdvert.push(data.data.module_ads.multi_block[16].data[0].child[0].pic);

		//每日尖货
		for(var i = 0,len=data.data.module_ads.multi_block[3].data[0].child.length;i < len;i++){
			var liveDayData = {};
			liveDayData.pId = "jianhuo" + i;
			liveDayData.pic_url = data.data.module_ads.multi_block[3].data[0].child[i].pic_url;
			liveDayData.cprice = data.data.module_ads.multi_block[3].data[0].child[i].cprice;
			liveDayData.oprice = data.data.module_ads.multi_block[3].data[0].child[i].oprice;
			liveDayData.title = data.data.module_ads.multi_block[3].data[0].child[i].title;
			liveDay.push(liveDayData);
		}
		//生活餐具
		for(var i = 5; i < 7;i++){
			for(var j = 0;j < 3;j++){
				liveDinner.push(data.data.module_ads.multi_block[i].data[0].child[j].pic);
			}
		}
		//清洁专区数据
		for(var i = 8; i < 10;i++){
			for(var j = 0;j < 3;j++){
				liveClear.push(data.data.module_ads.multi_block[i].data[0].child[j].pic);
			}
		}
		//数码家电
		for(var i = 11; i < 13;i++){
			for(var j = 0;j < 3;j++){
				liveTV.push(data.data.module_ads.multi_block[i].data[0].child[j].pic);
			}
		}
		//健康成长
		for(var i = 14; i < 16;i++){
			for(var j = 0;j < 3;j++){
				liveGrow.push(data.data.module_ads.multi_block[i].data[0].child[j].pic);
			}
		}
		liveMenus.liveAdvert = liveAdvert;
		liveMenus.liveModule = liveModule;
		liveMenus.liveDay = liveDay;
		liveMenus.liveDinner = liveDinner;
		liveMenus.liveClear = liveClear;
		liveMenus.liveTV = liveTV;
		liveMenus.liveGrow = liveGrow;
		def.resolve(liveMenus);
	});
	return def.promise;
})
.factory('Live', function ($http) {
     var Live = function () {
         this.items = [];
        this.busy = false;
         this.after = '';
         this.page = 6;

     };     
    Live.prototype.nextPage = function () {
         if (this.busy) return;
         this.busy = true;         
        var url = 'https://shop.juanpi.com/gsort?key=490&type=6&zhouyi_ids=p8_c3_l1_18_51_5&machining=hotcoupon&page='+ this.page +'&rows=10&dtype=JSONP&cm=1&cm_cate=490&callback=JSON_CALLBACK';
        $http.jsonp(url).success(function (data) {
             var items = data;
             for (var i = 0; i < items.list.length; i++) {
           			var liveGoodInfo = {};
           				liveGoodInfo.pId = 'goods' + i;
				 		liveGoodInfo.residue =  data.list[i].residue;
				 		liveGoodInfo.cprice = data.list[i].cprice;
				 		liveGoodInfo.oprice = data.list[i].oprice;
				 		liveGoodInfo.title = data.list[i].title;
				 		liveGoodInfo.pic_url  =  data.list[i].pic_url;
                 this.items.push(liveGoodInfo);
             }
             this.busy = false;
             this.page += 1;
         }.bind(this));
     }; 
     return Live;
 });
